//配置路由的地方
import Vue from "vue";
import VueRouter from "vue-router"
import Home from "@/pages/Home";
import Login from "@/pages/Login";
import Register from "@/pages/Register";
import Binding from "@/pages/Binding";
import Transfer from "@/pages/Transfer";
import Recharge from "@/pages/Recharge";
import Flow from "@/pages/Flow";
import Withdrawal from "@/pages/Withdrawal";
import UserCenter from "@/pages/UserCenter";
import PhoneTransfer from "@/pages/PhoneTransfer";
import NumberTransfer from "@/pages/NumberTransfer";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {getToken} from "@/utils/auth";
import {Message} from "element-ui";
//禁用进度环
NProgress.configure({ showSpinner: false })
Vue.use(VueRouter);

//重写push|replace，防止编程式路由跳转到当前路由（参数不变），多次执行会抛出NavigationDuplicated的错误警告
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
//重写push|replace
VueRouter.prototype.push = function (location,resolve,reject){
    if (resolve && reject){
        originPush.call(this,location,resolve,reject);
    }else {
        originPush.call(this,location,()=>{},()=>{});
    }
}
VueRouter.prototype.replace = function (location,resolve,reject){
    if (resolve && reject){
        originReplace.call(this,location,resolve,reject);
    }else {
        originReplace.call(this,location,()=>{},()=>{});
    }
}

//公共路由
let constantRoutes = [
    //登录组件
    {
      path: "/login",
      component: Login,
      meta:{
          hiddenNav: true
      }

    },
    //主页
    {
        path:"/home",
        component: Home
    },
    //注册
    {
        path:"/register",
        component: Register,
        meta: {
            hiddenNav: true
        }
    },
    //TODO 登录后加入动态路由，现在先暂时加进来
    //绑定
    {
        path:"/binding",
        component: Binding
    },
    //转账
    {
        path: "/transfer",
        component: Transfer
    },
    //用户名转账
    {
        path: "/numberTransfer",
        component: NumberTransfer
    },
    //手机号转账
    {
        path: "/phoneTransfer",
        component: PhoneTransfer
    },
    //交易流水查询
    {
        path: "/flow",
        component: Flow
    },
    //充值
    {
        path: "/recharge",
        component: Recharge
    },
    //提现
    {
        path: "/withdraw",
        component: Withdrawal
    },
    //个人信息
    {
      path: "/userCenter",
      component: UserCenter
    },
    {
        path: "/*",
        component: Login,
        meta: {
            hiddenNav: true
        }
    },
]

// //动态路由
// export let dynamicRoutes = [
//     //绑定
//     {
//         path:"/binding",
//         component: Binding
//     },
//     //转账
//     {
//         path: "/transfer",
//         component: Transfer
//     },
//     //交易流水查询
//     {
//         path: "/flow",
//         component: Flow
//     },
//     //充值
//     {
//         path: "/recharge",
//         component: Recharge
//     },
//     //提现
//     {
//         path: "/withdraw",
//         component: Withdrawal
//     }
// ]

let router = new VueRouter({
    mode: 'history',
    routes: constantRoutes
    //...
})

//TODO 路由守卫
//白名单
const whiteList = ['/login','/register','/'];
router.beforeEach((to,from,next)=>{
    // NProgress.start();
    //判断用户是否登录
    if(getToken()){
        //已经登录而且还想去登录------不行
        if(to.path === '/login' || to.path === '/register'){
            Message({
                message:'请先退出登录',
                type:'warning'
            })
            // location.href = '/home'
            next('/home')
            // NProgress.done()
        }else {
            next();
            // NProgress.done()
        }
    }else {
        //没有登录
        let toPath = to.path;
        console.log(toPath)
        if (whiteList.indexOf(toPath) !== -1){
            // 在免登录白名单，直接进入
            next()
            // NProgress.done()
        }else{
            Message({
                message:'请先登录',
                type:'warning'
            })
            // NProgress.done()
            next(`/login?redirect=${to.fullPath}`);
        }
    }
})



export default router;
